﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace PlutoStudio
{
    /// <summary>
    /// 
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToFirstLower(this string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return value;
            }
            return $"{value.Substring(0, 1).ToLower()}{value.Substring(1)}";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string MD5Hash(this string value)
        {
            
            return MD5Hash(value, Encoding.UTF8);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string MD5Hash(this string value, Encoding encoding)
        {
            return Hash(value, encoding, "MD5");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SHA1Hash(this string value)
        {
            return SHA1Hash(value, Encoding.UTF8);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string SHA1Hash(this string value, Encoding encoding)
        {
            return Hash(value, encoding, "SHA1");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static string Hash(this string value, Encoding encoding, string algorithmName)
        {
            var algorithm = HashAlgorithm.Create(algorithmName);
            var buffer = encoding.GetBytes(value);
            buffer = algorithm.ComputeHash(buffer);
            return BitConverter.ToString(buffer).Replace("-","");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="provider"></param>
        /// <param name="encoding"></param>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static string RSASignature(this string value, RSACryptoServiceProvider provider, Encoding encoding, string algorithmName)
        {
            var buffer = encoding.GetBytes(value);
            var result = provider.SignData(buffer, algorithmName);
            return Convert.ToBase64String(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="provider"></param>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static string RSASignature(this string value, RSACryptoServiceProvider provider, string algorithmName)
        {
            return RSASignature(value, provider, Encoding.UTF8, algorithmName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        public static string RSASignature(this string value, RSACryptoServiceProvider provider)
        {
            return RSASignature(value, provider, "md5");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="signature"></param>
        /// <param name="provider"></param>
        /// <param name="encoding"></param>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static bool RSAVerifySignature(this string value, string signature, RSACryptoServiceProvider provider, Encoding encoding, string algorithmName)
        {
            var buffer = encoding.GetBytes(value);
            var result = provider.VerifyData(buffer, algorithmName, Convert.FromBase64String(signature));
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="signature"></param>
        /// <param name="provider"></param>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static bool RSAVerifySignature(this string value, string signature, RSACryptoServiceProvider provider, string algorithmName)
        {
            return RSAVerifySignature(value, signature, provider, Encoding.UTF8, algorithmName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="signature"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        public static bool RSAVerifySignature(this string value, string signature, RSACryptoServiceProvider provider)
        {
            return RSAVerifySignature(value, signature, provider, "md5");
        }


    }
}
